import $http from './utils/request.js'
const api = require('./utils/api.js')
App({
  $http,
  globalData: {
    // host: 'http://192.168.137.1',
    // host: 'http://ruiwen.nat300.top',
    host: 'http://49.233.7.158'
  },

  getUserByToken() {
    console.log('version 2020-10-03 15:40')
    console.log('host=>' + this.globalData.host)
    const token = wx.getStorageSync('token')
    if (!token) {
      console.log('token不存在')
      wx.removeStorageSync('user')
    }
    const url = api.getUserByToken + `?token=${token}`
    $http.get(url).then(res => {
      if (res.code == 200) {
        console.log(res)
        wx.setStorageSync('user', res.data.user)
      } else {
        console.log('token无效')
        wx.removeStorageSync('user')
      }
    })
  },

  onLaunch() {
    $http.init(this)
    wx.setStorageSync('api', api)
    this.getUserByToken()
  },

  onError(msg) {
    console.log(msg)
    wx.showToast({
      title: 'wechat error',
      icon: 'none',
      duration: 2000
    })
  }
})